#include "card_table.h"
#include "../auth/auth.h"
#include "../auth/game.h"
#include "../utils/msg.h"

const lv_img_dsc_t *cards[] = {
    &spade_3,   &spade_4,   &spade_5,   &spade_6,   &spade_7,     &spade_8,
    &spade_9,   &spade_10,  &spade_J,   &spade_Q,   &spade_K,     &spade_A,
    &spade_2,   &heart_3,   &heart_4,   &heart_5,   &heart_6,     &heart_7,
    &heart_8,   &heart_9,   &heart_10,  &heart_J,   &heart_Q,     &heart_K,
    &heart_A,   &heart_2,   &club_3,    &club_4,    &club_5,      &club_6,
    &club_7,    &club_8,    &club_9,    &club_10,   &club_J,      &club_Q,
    &club_K,    &club_A,    &club_2,    &diamond_3, &diamond_4,   &diamond_5,
    &diamond_6, &diamond_7, &diamond_8, &diamond_9, &diamond_10,  &diamond_J,
    &diamond_Q, &diamond_K, &diamond_A, &diamond_2, &black_joker, &red_joker};

// 初始化lvgl线程锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t game_mutex = PTHREAD_MUTEX_INITIALIZER;
// 手牌数组
static int player_cards[20] = {0};
static lv_obj_t *hand_card[20];
static CARD send_card[20];
static lv_obj_t *opponent_card[20];
// 选择牌数
static int select_count = 0;

//选出地主标记
int lord_flag;

static pthread_t thread_id;

// 出牌区
static lv_obj_t *opponent_card_area;

// 房主标记
static int is_lord = 0;

// 牌桌页面
static lv_obj_t *table_page;

// 手牌区
static lv_obj_t *card_area;

// 出牌区
static lv_obj_t *send_card_area;

// 出牌按钮
static lv_obj_t *play_confirm;

// 退出按钮
static lv_obj_t *quit_btn;

// 要不起按钮
static lv_obj_t *pass_btn;

// 小窗口
static lv_obj_t *quit_win;

// 确认退出按钮
static lv_obj_t *quit_confirm_btn;

// 取消退出游戏按钮
static lv_obj_t *quit_cancel_btn;

// 抢地主按钮
static lv_obj_t *roblard_btn;

// 叫地主按钮
static lv_obj_t *callard_btn;

// 准备游戏按钮
static lv_obj_t *ready_btn;

// 取消准备按钮
static lv_obj_t *unready_btn;

// 开始游戏按钮(房主准备按钮)
static lv_obj_t *action_btn;

//对手1区域
static lv_obj_t *opponent1_area;
static lv_obj_t *opponent1_text;
static lv_obj_t *card_bankground_player1;
static lv_obj_t *opponent1_lable = NULL;

//对手2区域
static lv_obj_t *opponent2_area;
static lv_obj_t *opponent2_text;
static lv_obj_t *card_bankground_player2;
static lv_obj_t *opponent2_lable = NULL;

static lv_obj_t *lord_lable;

static bool card_click_action(lv_event_t *e);

int get_player_id_from_struct() {
    return curr_gameStatus.players[curr_gameStatus.who].id;
}

void show_other_player(){
        if(is_lord){
            if(curr_gameStatus.players[1].id != -1){
                lv_label_set_text(opponent1_lable, curr_gameStatus.players[1].username);
                lv_obj_align(opponent1_text, LV_ALIGN_CENTER, 0, 0);
            }
            if(curr_gameStatus.players[2].id != -1){
                lv_label_set_text(opponent2_lable, curr_gameStatus.players[2].username);
                lv_obj_align(opponent2_text, LV_ALIGN_CENTER, 0, 0);
            }
        }else{
            lv_label_set_text(opponent1_lable, curr_gameStatus.players[0].username);
            lv_obj_align(opponent1_text, LV_ALIGN_CENTER, 0, 0);
            if(curr_gameStatus.players[1].id != current_user.id){
                lv_label_set_text(opponent2_lable, curr_gameStatus.players[1].username);
                lv_obj_align(opponent2_text, LV_ALIGN_CENTER, 0, 0);
            }else{
                lv_label_set_text(opponent2_lable, curr_gameStatus.players[2].username);
                lv_obj_align(opponent2_text, LV_ALIGN_CENTER, 0, 0);
            }
        }
        
    
}

void show_lord(){
    if(lord_flag == 1){
        lv_obj_clear_flag(lord_lable,LV_OBJ_FLAG_HIDDEN);
        for(int i = 0; i < 3; i++){
        if(curr_gameStatus.players[curr_gameStatus.who_load].id == current_user.id){
            lv_obj_align_to(lord_lable,card_area, LV_ALIGN_BOTTOM_LEFT, 0, 25);
            return;
        }
        if(curr_gameStatus.players[curr_gameStatus.who_load].id == curr_gameStatus.players[i].id){
            lv_obj_align_to(lord_lable,card_bankground_player1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
            return;
        }else{
            lv_obj_align_to(lord_lable,card_bankground_player2, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
            return;
        }
        if(is_lord){
            if(curr_gameStatus.who_load == 1){
                lv_obj_align_to(lord_lable,card_bankground_player1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
                return; 
            }else if(curr_gameStatus.who_load == 2){
                lv_obj_align_to(lord_lable,card_bankground_player2, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
                return; 
            }
            
        }
    }

    }
}

// 获取手牌
void get_cards() {
    
    for(int i = 0; i < 20; i++){
        if(hand_card[i] != NULL){
            lv_obj_del(hand_card[i]);
            player_cards[i] = -1;
            hand_card[i] = NULL;
        }
    }

    for (int i = 0; i < curr_gameStatus.handcard.len; i++) {
        
        if (hand_card[i] == NULL) {
            player_cards[i] = curr_gameStatus.handcard.data[i];
            lv_obj_t *imgbtn1 = lv_imgbtn_create(lv_scr_act());
            lv_obj_set_size(imgbtn1, 100, 150);
            lv_imgbtn_set_src(imgbtn1, LV_IMGBTN_STATE_RELEASED, NULL,
                              cards[player_cards[i]], NULL);
            lv_obj_align_to(imgbtn1, card_area, LV_ALIGN_OUT_BOTTOM_LEFT,
                            25 * i, 0);
            lv_obj_add_event_cb(imgbtn1, card_click_action, LV_EVENT_CLICKED,
                                NULL);
            lv_obj_set_user_data(imgbtn1, &player_cards[i]);
            hand_card[i] = imgbtn1;
            // usleep(100000);
            int *userdata = lv_obj_get_user_data(hand_card[i]);
            
        }       
    }
    if(curr_gameStatus.status == GAME_STATUS_GAMEING){
        lord_flag = 1;
    }
}

// 更新屏幕上上家的牌
void destory_card() {
    int send_count = 0;
    int remain_count = 0;
    if (get_player_id_from_struct() == current_user.id) {
        for (int i = 0; i < 20; i++) {
            if (opponent_card[i] != NULL) {
                lv_obj_del(opponent_card[i]);
                opponent_card[i] = NULL;
            }
            if (hand_card[i] != NULL) {
                if (lv_obj_has_flag(hand_card[i], LV_OBJ_FLAG_BUTTON_PRESSED)) {
                    lv_obj_align_to(hand_card[i], send_card_area,
                                    LV_ALIGN_CENTER, send_count * 25, 0);
                    send_count++;
                } else {
                    lv_obj_align_to(hand_card[i], card_area,
                                    LV_ALIGN_OUT_BOTTOM_LEFT, remain_count * 25,
                                    0);
                    remain_count++;
                }
            }
            if (hand_card[i] != NULL) {
                if (lv_obj_has_flag(hand_card[i], LV_OBJ_FLAG_BUTTON_PRESSED)) {
                    opponent_card[i] = hand_card[i];
                    int *oppen = lv_obj_get_user_data(opponent_card[i]);
                    int *hand = lv_obj_get_user_data(hand_card[i]);
                    lv_obj_set_user_data(opponent_card[i], NULL);
                    hand_card[i] = NULL;
                }
            }
        }
        select_count = 0;
        lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
    } else {
        for (int i = 0; i < 20; i++) {
            if (opponent_card[i] != NULL) {
                lv_obj_del(opponent_card[i]);
                opponent_card[i] = NULL;
            }
        }
        for (int i = 0; i < 20; i++) {
            if (curr_gameStatus.cards.len != 0 &&
                i < curr_gameStatus.cards.len) {
                lv_obj_t *imgbtn1 = lv_imgbtn_create(table_page);
                lv_obj_set_size(imgbtn1, 100, 150);
                lv_imgbtn_set_src(imgbtn1, LV_IMGBTN_STATE_RELEASED, NULL,
                                  cards[curr_gameStatus.cards.data[i]], NULL);
                lv_obj_align_to(imgbtn1, send_card_area, LV_ALIGN_CENTER,
                                i * 25, 0);
                opponent_card[i] = imgbtn1;
            }
        }
    }
    memset(send_card, -1, sizeof(send_card));
}

// 等待玩家
void wait_player(bool is_lord) {
    if (is_lord) { // 如果是房主
        int ready_count = 0;
        for (int i = 0; i < 3; i++) {
            if (curr_gameStatus.players[i].id == -1) {
                continue;
            }
            if (curr_gameStatus.players[i].status == PLAYER_STATUS_READY) {
                ready_count++;
            }
        }
        if (ready_count > 1 &&
            lv_obj_has_flag(action_btn,
                            LV_OBJ_FLAG_HIDDEN)) { // 显示开始游戏按钮
            lv_obj_clear_flag(action_btn, LV_OBJ_FLAG_HIDDEN);
        }
    } else { // 如果不是房主
        if (lv_obj_has_flag(ready_btn, LV_OBJ_FLAG_HIDDEN) &&
            lv_obj_has_flag(unready_btn,
                            LV_OBJ_FLAG_HIDDEN)) { // 显示开始游戏按钮
            lv_obj_clear_flag(ready_btn, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

// 抢地主
int robload() {
    int callord_flag = 0;
    if (get_player_id_from_struct() == current_user.id) { // 如果是自己的回合
        if (lv_obj_has_flag(callard_btn, LV_OBJ_FLAG_HIDDEN) &&
            lv_obj_has_flag(pass_btn, LV_OBJ_FLAG_HIDDEN)) {
            lv_obj_clear_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        }
        // 如果有人点过叫地主则之后的人只显示抢地主
        for (int i = 0; i < 3; i++) {
            if (curr_gameStatus.rob_status[i] > 0) {
                callord_flag = 1;
            }
        }
        if (lv_obj_has_flag(callard_btn, LV_OBJ_FLAG_BUTTON_PRESSED) ||
            callord_flag == 1) {
            lv_obj_add_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(roblard_btn, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

// 游戏中
void gaming() {
    int who = curr_gameStatus.who;
    if (get_player_id_from_struct() == current_user.id) { // 如果是自己的回合
        // log_info("my turn");
        // lv_obj_clear_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        memset(send_card, -1, sizeof(send_card));

        // 显示要不起按钮
        if (lv_obj_has_flag(pass_btn, LV_OBJ_FLAG_HIDDEN) &&
            (!lv_obj_has_flag(pass_btn, ISHIDDEN))) {
            lv_obj_clear_flag(
                play_confirm,
                LV_OBJ_FLAG_BUTTON_PRESSED); // 同时更新出牌按钮点击状态
            lv_obj_clear_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(pass_btn, ISHIDDEN);
        }
        for (int i = 0; i < curr_gameStatus.cards.len; i++) {//自己回合开始时清空更新屏幕上的牌
            if (opponent_card[i] != NULL) {
                lv_obj_del(opponent_card[i]);
                opponent_card[i] = NULL;
            }
            if (curr_gameStatus.cards.len != 0 &&
                i < curr_gameStatus.cards.len) {
                lv_obj_t *imgbtn1 = lv_imgbtn_create(table_page);
                lv_obj_set_size(imgbtn1, 100, 150);
                lv_imgbtn_set_src(imgbtn1, LV_IMGBTN_STATE_RELEASED, NULL,
                                  cards[curr_gameStatus.cards.data[i]], NULL);
                lv_obj_align_to(imgbtn1, send_card_area, LV_ALIGN_CENTER,
                                i * 25, 0);
                opponent_card[i] = imgbtn1;
            }
        }
    }
}

// 确定退出按钮回调函数
bool quit_comfirm_cb() {
    cJSON *root = cJSON_CreateObject();
    pthread_mutex_lock(&game_mutex);
    cJSON_AddNumberToObject(root, "user_id", current_user.id);
    cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
    response_t *response =
        request(MEVEN_HTTP_POST, "/game/quit_game", NULL, root);
    pthread_mutex_unlock(&game_mutex);
    if (response == NULL) {
        // msg_warn("req error",3000000);
        cJSON_Delete(root);
        return false;
    }
    if (response->code != RESPONSE_OK) {
        // 退出失败处理
        cJSON_Delete(root);
        response_free(response);
        return false;
    }
    response_free(response);
    table_page_destroy();
    game_room_list_page_display();
}

// 取消退出回调函数
bool quit_cancel_cb() {
    if (quit_win != NULL) {
        lv_obj_add_flag(quit_win, LV_OBJ_FLAG_HIDDEN);
        if (select_count > 0) {
            lv_obj_clear_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

// 遍历出牌数组
void traverse_obj() {
    for (int i = 0; i < 20; i++) {
        if (hand_card[i] != NULL) {
            if (lv_obj_has_flag(hand_card[i], LV_OBJ_FLAG_BUTTON_PRESSED)) {
                int *user_data = lv_obj_get_user_data(hand_card[i]);
                send_card[i] = *user_data;
            }
        }
    }
}

// 点击卡牌
bool card_click_action(lv_event_t *e) {
    if (get_player_id_from_struct() == current_user.id &&
        !(lv_obj_has_flag(quit_btn, LV_OBJ_FLAG_BUTTON_PRESSED)) &&
        curr_gameStatus.status ==
            GAME_STATUS_GAMEING) { // 在正式开始游戏后自己回合才能选，点击退出后不能选
        lv_obj_t *obj = lv_event_get_target(e);
        void *user_data = lv_obj_get_user_data(obj);
        int *card_value = (int *)user_data;
        if (!lv_obj_has_flag(obj, LV_OBJ_FLAG_BUTTON_PRESSED)) {
            lv_obj_set_y(obj, lv_obj_get_y(obj) - 25);
            lv_obj_add_flag(obj, LV_OBJ_FLAG_BUTTON_PRESSED);
            if (select_count == 0) {
                lv_obj_clear_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
            }
            select_count++;
        } else {
            lv_obj_set_y(obj, lv_obj_get_y(obj) + 25);
            lv_obj_clear_flag(obj, LV_OBJ_FLAG_BUTTON_PRESSED);
            if (select_count == 1) {
                lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
            }
            select_count--;
        }
    }
}

// 出牌
bool play() {
    traverse_obj();
    cJSON *root = cJSON_CreateObject();
    pthread_mutex_lock(&game_mutex);
    cJSON_AddStringToObject(root, "username", current_user.username);
    cJSON_AddNumberToObject(root, "user_id", current_user.id);
    cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
    pthread_mutex_unlock(&game_mutex);
    cJSON *card = cJSON_CreateArray();
    for (int i = 0; i < 20; i++) {
        if (send_card[i] != -1) {
            cJSON *item1 = cJSON_CreateNumber(send_card[i]);
            cJSON_AddItemToArray(card, item1);
        }
    }
    cJSON_AddItemToObject(root, "cards", card);
    response_t *response =
        request(MEVEN_HTTP_POST, "/game/cardout", NULL, root);
    cJSON_Delete(root);
    if (response == NULL) {
        msg_warn("req error", 3000000);
        return false;
    }
    if (response->code != RESPONSE_OK) {
        // 处理出牌结果,记得释放内存
        memset(send_card, -1, sizeof(send_card));
        response_free(response);
        return;
    }
    response_free(response);
    destory_card();
    memset(send_card, -1, sizeof(send_card));
    lv_obj_clear_flag(pass_btn, ISHIDDEN);
    lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
}

// 退出游戏按钮
bool quit_game(lv_event_t *e) {
    lv_obj_add_flag(quit_btn, LV_OBJ_FLAG_BUTTON_PRESSED);
    lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
    // 点击退出后显示小页面
    if (quit_win == NULL) {

        // 创建退出游戏小页面
        quit_win = lv_obj_create(lv_scr_act());
        // 设置界面大小
        lv_obj_set_size(quit_win, JOIN_GAME_PAGE_WIDTH, JOIN_GAME_PAGE_HEIGHT);
        lv_obj_set_pos(quit_win, 235, 120);

        // 在创建房间小页面中添加文本
        lv_obj_t *text = lv_label_create(quit_win);
        lv_label_set_text(text, "are you sure quit game");
        lv_obj_align(text, LV_ALIGN_CENTER, 0, 0);
        // 在创建房间小页面中添加确定按钮
        quit_confirm_btn = lv_btn_create(quit_win);
        lv_obj_set_size(quit_confirm_btn, 100, 50);
        lv_obj_align(quit_confirm_btn, LV_ALIGN_BOTTOM_LEFT, 0, 0);
        // 设置事件，传递输入框数据
        lv_obj_add_event_cb(quit_confirm_btn, quit_comfirm_cb, LV_EVENT_CLICKED,
                            NULL);
        // 设置按钮标签
        lv_obj_t *create_confirm_small_label =
            lv_label_create(quit_confirm_btn);
        lv_obj_align_to(create_confirm_small_label, quit_confirm_btn,
                        LV_ALIGN_LEFT_MID, 0, 0);
        lv_obj_set_style_text_font(create_confirm_small_label,
                                   &lv_font_montserrat_16, 0);
        lv_label_set_text(create_confirm_small_label, "confirm");
        // 在创建房间小页面中添加取消按钮
        quit_cancel_btn = lv_btn_create(quit_win);
        lv_obj_set_size(quit_cancel_btn, 100, 50);
        lv_obj_align(quit_cancel_btn, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
        // 设置事件，传递输入框数据
        lv_obj_add_event_cb(quit_cancel_btn, quit_cancel_cb, LV_EVENT_CLICKED,
                            NULL);
        // 设置按钮标签
        lv_obj_t *create_cancel_small_label = lv_label_create(quit_cancel_btn);
        lv_obj_align_to(create_cancel_small_label, quit_cancel_btn,
                        LV_ALIGN_LEFT_MID, 0, 0);
        lv_obj_set_style_text_font(create_cancel_small_label,
                                   &lv_font_montserrat_16, 0);
        lv_label_set_text(create_cancel_small_label, "cancel");
    }
    // 这里做弹出小界面的逻辑
    lv_obj_move_foreground(quit_win);
    lv_obj_clear_flag(quit_win, LV_OBJ_FLAG_HIDDEN);
}

//收回手牌
void retract_card(){
    for(int i = 0; i < curr_gameStatus.handcard.len; i++){
        if(hand_card[i] != NULL && lv_obj_has_flag(hand_card[i],LV_OBJ_FLAG_BUTTON_PRESSED)){
            lv_obj_set_y(hand_card[i], lv_obj_get_y(hand_card[i]) + 25);
            lv_obj_clear_flag(hand_card[i], LV_OBJ_FLAG_BUTTON_PRESSED);
            select_count--;
        }
    }
}

// 要不起
bool pass_cb(lv_event_t *e) {
    if (curr_gameStatus.status == GAME_STATUS_GAMEING) { // 游戏出牌阶段的pass
        lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_BUTTON_PRESSED);
        cJSON *root = cJSON_CreateObject();
        pthread_mutex_lock(&game_mutex);
        cJSON_AddNumberToObject(root, "user_id", current_user.id);
        cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
        cJSON_AddStringToObject(root, "username", current_user.username);
        pthread_mutex_unlock(&game_mutex);
        response_t *resp =
            request(MEVEN_HTTP_POST, "/game/cardoutpass", NULL, root);
        if (resp->code == RESPONSE_OK) {
            retract_card();
            response_free(resp);
            lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);
            cJSON_Delete(root);
            return true;
        } else {
            // 处理
            retract_card();
            cJSON_Delete(root);
            response_free(resp);
            perror("rep error");
            return false;
        }
    } else { // 抢地主过程中的pass
        lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_BUTTON_PRESSED);
        cJSON *root = cJSON_CreateObject();
        pthread_mutex_lock(&game_mutex);
        cJSON_AddNumberToObject(root, "user_id", current_user.id);
        cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
        cJSON_AddStringToObject(root, "username", current_user.username);
        cJSON_AddNumberToObject(root, "status", ROB_LOAD_PASS);
        pthread_mutex_unlock(&game_mutex);

        response_t *resp =
            request(MEVEN_HTTP_POST, "/game/robload", NULL, root);
        if (resp == NULL) {
            msg_warn("req error", 3000000);
            return false;
        }
        if (resp->code == RESPONSE_OK) {
            response_free(resp);
            lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(roblard_btn, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);
            cJSON_Delete(root);
            return true;
        } else {
            // 处理
            response_free(resp);
            perror("rep error");
            cJSON_Delete(root);
            return false;
        }
    }
}

// 抢地主
bool roblord_cb(lv_event_t *e) {
    cJSON *root = cJSON_CreateObject();
    pthread_mutex_lock(&game_mutex);
    cJSON_AddNumberToObject(root, "user_id", current_user.id);
    cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
    cJSON_AddStringToObject(root, "username", current_user.username);
    cJSON_AddNumberToObject(root, "status", ROB_LOAD_ROB);
    pthread_mutex_unlock(&game_mutex);
    response_t *resp = request(MEVEN_HTTP_POST, "/game/robload", NULL, root);
    cJSON_Delete(root);
    if (resp == NULL) {
        return false;
    }
    if (resp->code == RESPONSE_OK) {
        response_free(resp);
        lv_obj_add_flag(roblard_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);
        return true;
    } else {
        response_free(resp);
        return false;
    }
}

// 叫地主
bool callord_cb(lv_event_t *e) {
    pthread_mutex_lock(&game_mutex);
    cJSON *root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "user_id", current_user.id);
    cJSON_AddNumberToObject(root, "game_id", curr_gameStatus.id);
    cJSON_AddStringToObject(root, "username", current_user.username);
    cJSON_AddNumberToObject(root, "status", ROB_LOAD_CALL);
    pthread_mutex_unlock(&game_mutex);
    response_t *resp = request(MEVEN_HTTP_POST, "/game/robload", NULL, root);
    cJSON_Delete(root);
    if (resp == NULL) {
        // msg_warn("req error",3000000);
        return false;
    }
    if (resp->code == RESPONSE_OK) {
        response_free(resp);
        lv_obj_add_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(roblard_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        return true;
    } else {
        // 处理
        response_free(resp);
        perror("req error");
        return false;
    }
}

// 准备按钮
bool ready_btn_cb() {
    pthread_mutex_lock(&game_mutex);
    response_t *resp = set_player_status(PLAYER_STATUS_READY);
    if (resp == NULL) {
        msg_warn("req error", 3000000);
        return false;
    }
    pthread_mutex_unlock(&game_mutex);
    if (resp->code == RESPONSE_OK) {
        response_free(resp);
        lv_obj_clear_flag(unready_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ready_btn, LV_OBJ_FLAG_HIDDEN);
    } else {
        // 处理
        response_free(resp);
        perror("response error");
        return false;
    }
}

// 取消准备按钮
bool unready_btn_cb() {
    pthread_mutex_lock(&game_mutex);
    response_t *resp = set_player_status(PLAYER_STATUS_NONE);
    if (resp == NULL) {
        msg_warn("req error", 3000000);
        return false;
    }
    pthread_mutex_unlock(&game_mutex);
    if (resp->code == RESPONSE_OK) {
        lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ready_btn,LV_OBJ_FLAG_HIDDEN);
        response_free(resp);
        return true;
    } else {
        // 处理
        response_free(resp);
        perror("response error");
        return false;
    }
}

// 开始游戏
bool action_btn_cb() {
    pthread_mutex_lock(&game_mutex);
    response_t *resp = set_player_status(PLAYER_STATUS_READY);
    if (resp == NULL) {
        msg_warn("req error", 3000000);
        return false;
    }
    pthread_mutex_unlock(&game_mutex);
    if (resp->code == RESPONSE_OK) {

        lv_obj_add_flag(action_btn, LV_OBJ_FLAG_HIDDEN);
        response_free(resp);
        return true;
    } else {
        response_free(resp);
        // 处理
        perror("response error");
        return false;
    }
}

//游戏线程
void *game_thread(void *arg) {
    int last_who = -1;
    int last_len = -1;
    int player_count = 0;
    lord_flag = 0;
    if (curr_gameStatus.id == current_user.id) {
        is_lord = 1;
    }
    // log_info("loop");
    while (1) {
        usleep(500000);
        pthread_mutex_lock(&game_mutex);
        // 更新游戏状态
        get_gameStatus(curr_gameStatus.id);
        pthread_mutex_lock(&mutex);
        show_lord();
        pthread_mutex_unlock(&mutex);
        if(curr_gameStatus.player_count != player_count || curr_gameStatus.who != last_who){
                show_other_player();
        }
        player_count = curr_gameStatus.player_count;
        if (curr_gameStatus.who != last_who ||
            curr_gameStatus.handcard.len != last_len) {
            pthread_mutex_lock(&mutex);
            destory_card();
            get_cards();
            pthread_mutex_unlock(&mutex);
            last_who = curr_gameStatus.who;
            last_len = curr_gameStatus.handcard.len;
            
        }
        pthread_mutex_unlock(&game_mutex);
        pthread_mutex_lock(&mutex);
        switch (curr_gameStatus.status) {
        case GAME_STATUS_WAIT_PLAYER:
            wait_player(is_lord);
            break;
        case GAME_STATUS_CALLLOAD:
            if (!lv_obj_has_flag(unready_btn, LV_OBJ_FLAG_HIDDEN)) {
                lv_obj_add_flag(unready_btn, LV_OBJ_FLAG_HIDDEN);
            }
            robload();
            break;
        case GAME_STATUS_ROBLOAD:
            robload();
            break;
        case GAME_STATUS_GAMEING:
            gaming();
            break;
        case GAME_STATUS_END:
            last_who = -1;
            last_len = -1;
            player_count = 0;
            lord_flag = 0;
            break;
        case GAME_STATUS_START:
            break;
        default:
            break;
        }
        pthread_mutex_unlock(&mutex);
    }
}

void table_page_display() {
    msg_init();
    memset(hand_card, 0, sizeof(hand_card));
    memset(opponent_card, 0, sizeof(opponent_card));
    memset(player_cards, -1, sizeof(player_cards));

    // 创建欢迎页面
    table_page = lv_obj_create(lv_scr_act());

    // 设置界面大小
    lv_obj_set_size(table_page, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_radius(table_page, 0, 0);
    lv_obj_set_style_border_width(table_page, 0, 0);
    lv_obj_set_style_outline_width(table_page, 0, 0);

    // 创建背景对象
    static lv_style_t style_bg;
    lv_style_init(&style_bg);
    lv_style_set_bg_img_src(&style_bg, &game_interface);
    lv_obj_add_style(table_page, &style_bg, LV_PART_MAIN);

    // 创建出牌区
    send_card_area = lv_obj_create(lv_scr_act());
    lv_obj_set_size(send_card_area, 0, 0);
    lv_obj_align(send_card_area, LV_ALIGN_OUT_BOTTOM_MID, 200, 100);

    // 创建手牌区
    card_area = lv_obj_create(lv_scr_act());
    lv_obj_set_size(card_area, 0, 0);
    lv_obj_align(card_area, LV_ALIGN_OUT_BOTTOM_MID, 100, 300);

    // 对手1
    opponent1_area = lv_obj_create(table_page);
    lv_obj_set_size(opponent1_area, 0, 0);
    lv_obj_align(table_page, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_add_flag(opponent1_area, LV_OBJ_FLAG_HIDDEN);

    card_bankground_player1 = lv_img_create(lv_scr_act());
    lv_img_set_src(card_bankground_player1,
                   &Background); // 设置图像对象的图像源
    lv_obj_align(table_page, LV_ALIGN_LEFT_MID, 0,
                 0); // 设置图像对象在父对象中的对齐方式
    lv_img_set_antialias(card_bankground_player1, true);
    lv_obj_set_size(card_bankground_player1, 100, 150); // 设置图像对象的大小

    // 对手2
    opponent2_area = lv_obj_create(table_page);
    lv_obj_set_size(opponent2_area, 0, 0);
    lv_obj_align(opponent2_area, LV_ALIGN_TOP_RIGHT, -105, 50);
    lv_obj_add_flag(opponent2_area, LV_OBJ_FLAG_HIDDEN);

    card_bankground_player2 = lv_img_create(lv_scr_act());
    lv_img_set_src(card_bankground_player2,
                   &Background); // 设置图像对象的图像源
    lv_obj_align_to(card_bankground_player2, opponent2_area, LV_ALIGN_LEFT_MID,
                    0, 0); // 设置图像对象在父对象中的对齐方式
    lv_img_set_antialias(card_bankground_player2, true);
    lv_obj_set_size(card_bankground_player2, 100, 150); // 设置图像对象的大小

    //对手1
    opponent1_text = lv_obj_create(card_bankground_player1);
    lv_obj_set_size(opponent1_text, 75, 75);
    lv_obj_align_to(opponent1_text,card_bankground_player1, LV_ALIGN_CENTER, 0, 0);
    opponent1_lable = lv_label_create(opponent1_text);

    //对手2
    opponent2_text = lv_obj_create(card_bankground_player2);
    lv_obj_set_size(opponent2_text, 75, 75);
    lv_obj_align_to(opponent2_text,card_bankground_player2, LV_ALIGN_CENTER, 0, 0);
    opponent2_lable = lv_label_create(opponent2_text);

    //地主图标
    lord_lable = lv_img_create(table_page);
    lv_img_set_src(lord_lable,
                   &lord_tag);
    lv_obj_align_to(lord_lable,card_area, LV_ALIGN_BOTTOM_LEFT, 0, 25);
    lv_obj_add_flag(lord_lable,LV_OBJ_FLAG_HIDDEN);

    // 出牌按钮
    play_confirm = lv_btn_create(lv_scr_act());
    lv_obj_set_size(play_confirm, 100, 50);
    lv_obj_align(play_confirm, LV_ALIGN_CENTER, 110, 25);
    lv_obj_add_event_cb(play_confirm, play, LV_EVENT_CLICKED, NULL);
    lv_obj_t *play_label = lv_label_create(play_confirm);
    lv_obj_align_to(play_label, play_confirm, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(play_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(play_label, "play");
    lv_obj_add_flag(play_confirm, LV_OBJ_FLAG_HIDDEN);

    // 退出游戏按钮
    quit_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(quit_btn, 100, 50);
    lv_obj_align(quit_btn, LV_ALIGN_LEFT_MID, 700, 200);
    lv_obj_add_event_cb(quit_btn, quit_game, LV_EVENT_CLICKED, NULL);
    lv_obj_t *quite_label = lv_label_create(quit_btn);
    lv_obj_align_to(quite_label, quit_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(quite_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(quite_label, "quit");

    // 要不起按钮（不抢地主按钮）
    pass_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(pass_btn, 100, 50);
    lv_obj_align(pass_btn, LV_ALIGN_CENTER, -110, 25);
    lv_obj_add_event_cb(pass_btn, pass_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *pass_label = lv_label_create(pass_btn);
    lv_obj_align_to(pass_label, pass_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(pass_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(pass_label, "pass");
    lv_obj_add_flag(pass_btn, LV_OBJ_FLAG_HIDDEN);

    // 抢地主按钮
    roblard_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(roblard_btn, 100, 50);
    lv_obj_align(roblard_btn, LV_ALIGN_CENTER, 110, 25);
    lv_obj_add_event_cb(roblard_btn, roblord_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *roblord_label = lv_label_create(roblard_btn);
    lv_obj_align_to(roblord_label, roblard_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(roblord_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(roblord_label, "roblord");
    lv_obj_add_flag(roblard_btn, LV_OBJ_FLAG_HIDDEN);

    // 叫地主按钮
    callard_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(callard_btn, 100, 50);
    lv_obj_align(callard_btn, LV_ALIGN_CENTER, 0, 25);
    lv_obj_add_event_cb(callard_btn, callord_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *callord_label = lv_label_create(callard_btn);
    lv_obj_align_to(callord_label, callard_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(callord_label, &lv_font_montserrat_16, 0);
    lv_label_set_text(callord_label, "callord");
    lv_obj_add_flag(callard_btn, LV_OBJ_FLAG_HIDDEN);

    // 准备按钮
    ready_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(ready_btn, 100, 50);
    lv_obj_align(ready_btn, LV_ALIGN_CENTER, 110, 25);
    lv_obj_add_event_cb(ready_btn, ready_btn_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *ready_btn_lable = lv_label_create(ready_btn);
    lv_obj_align_to(ready_btn_lable, ready_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(ready_btn_lable, &lv_font_montserrat_16, 0);
    lv_label_set_text(ready_btn_lable, "ready");
    lv_obj_add_flag(ready_btn, LV_OBJ_FLAG_HIDDEN);

    // 取消准备按钮
    unready_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(unready_btn, 100, 50);
    lv_obj_align(unready_btn, LV_ALIGN_CENTER, -110, 25);
    lv_obj_add_event_cb(unready_btn, unready_btn_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *unready_btn_lable = lv_label_create(unready_btn);
    lv_obj_align_to(unready_btn_lable, unready_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(unready_btn_lable, &lv_font_montserrat_16, 0);
    lv_label_set_text(unready_btn_lable, "unready");
    lv_obj_add_flag(unready_btn, LV_OBJ_FLAG_HIDDEN);

    // 开始按钮
    action_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(action_btn, 100, 50);
    lv_obj_align(action_btn, LV_ALIGN_CENTER, 50, 25);
    lv_obj_add_event_cb(action_btn, action_btn_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *action_btn_lable = lv_label_create(action_btn);
    lv_obj_align_to(action_btn_lable, action_btn, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_style_text_font(action_btn_lable, &lv_font_montserrat_16, 0);
    lv_label_set_text(action_btn_lable, "action");
    lv_obj_add_flag(action_btn, LV_OBJ_FLAG_HIDDEN);

    pthread_create(&thread_id, NULL, game_thread, NULL);
}

void table_page_destroy() {
    pthread_cancel(thread_id);
    // 等待线程结束
    pthread_join(thread_id, NULL);
    lv_obj_del(table_page);
}